const axios = require('axios');

// 测试Google OAuth2流程
async function testGoogleOAuth() {
  console.log('🔑 Google OAuth2 测试');
  console.log('========================');
  
  // 1. 生成授权URL
  const clientId = '82716260351-eunjm8imnr6pc4djgnoaa9mvdoa08nsh.apps.googleusercontent.com';
  const redirectUri = 'http://localhost:3000/auth/google/callback';
  const scope = 'openid email profile';
  
  const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +
    `client_id=${clientId}&` +
    `redirect_uri=${encodeURIComponent(redirectUri)}&` +
    `scope=${encodeURIComponent(scope)}&` +
    `response_type=code&` +
    `access_type=offline&` +
    `prompt=consent`;
  
  console.log('📋 步骤1: 在浏览器中访问以下URL进行授权');
  console.log(authUrl);
  console.log('\n📋 步骤2: 授权后，您会被重定向到回调URL');
  console.log('回调URL格式: http://localhost:3000/auth/google/callback?code=AUTHORIZATION_CODE');
  console.log('\n📋 步骤3: 从URL中提取授权码(code参数)');
  console.log('\n📋 步骤4: 使用以下命令获取Access Token:');
  
  console.log('\ncurl -X POST https://oauth2.googleapis.com/token \\');
  console.log('  -H "Content-Type: application/x-www-form-urlencoded" \\');
  console.log('  -d "client_id=' + clientId + '" \\');
  console.log('  -d "client_secret=YOUR_CLIENT_SECRET" \\');
  console.log('  -d "code=AUTHORIZATION_CODE" \\');
  console.log('  -d "grant_type=authorization_code" \\');
  console.log('  -d "redirect_uri=' + redirectUri + '"');
  
  console.log('\n⚠️  重要提示:');
  console.log('- 需要从Google Cloud Console获取Client Secret');
  console.log('- 确保重定向URI已在Google Cloud Console中配置');
  console.log('- 授权码只能使用一次，过期时间很短');
  console.log('- 建议保存Refresh Token以便后续使用');
  
  console.log('\n🔧 替代方案:');
  console.log('如果无法获取Client Secret，可以使用Google OAuth2 Playground:');
  console.log('https://developers.google.com/oauthplayground/');
  console.log('1. 点击右上角设置图标');
  console.log('2. 勾选"Use your own OAuth credentials"');
  console.log('3. 输入Client ID和Client Secret');
  console.log('4. 选择API范围: userinfo.email, userinfo.profile');
  console.log('5. 点击"Authorize APIs"进行授权');
  console.log('6. 点击"Exchange authorization code for tokens"');
  console.log('7. 复制生成的Access Token');
}

// 运行测试
testGoogleOAuth(); 